<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="68px">
            <el-form-item label="" prop="orderNo">
                <el-input v-model="queryParams.orderNo" placeholder="请输入订单编号" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="userName">
                <el-input v-model="queryParams.userName" placeholder="请输入用户姓名" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="mobile">
                <el-input v-model="queryParams.mobile" placeholder="请输入用户手机号" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="address">
                <el-input v-model="queryParams.address" placeholder="请输入用户地址" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="salesman">
                <el-input v-model="queryParams.salesman" placeholder="请输入业务员姓名" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>

            <el-form-item label="" prop="measureFlag">
                <el-select v-model="queryParams.measureFlag" placeholder="是否包含测量" clearable>
                    <el-option v-for="dict in dict.type.measure_flag" :key="dict.value" :label="dict.label"
                        :value="dict.value" />
                </el-select>
            </el-form-item>
            <el-form-item label="" prop="installFlag">
                <el-select v-model="queryParams.installFlag" placeholder="是否包含安装" clearable>
                    <el-option v-for="dict in dict.type.install_flag" :key="dict.value" :label="dict.label"
                        :value="dict.value" />
                </el-select>
            </el-form-item>
            <el-form-item label="" prop="expressFlag">
                <el-select v-model="queryParams.expressFlag" placeholder="是否包含物流" clearable>
                    <el-option v-for="dict in dict.type.express_flag" :key="dict.value" :label="dict.label"
                        :value="dict.value" />
                </el-select>
            </el-form-item>
            <el-form-item label="" prop="expressFlag">
                <el-select v-model="queryParams.status" placeholder="订单状态" clearable>
                    <el-option v-for="dict in dict.type.order_status" :key="dict.value" :label="dict.label"
                        :value="dict.value" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['aftersales:order:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['aftersales:order:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                    @click="handleDelete" v-hasPermi="['aftersales:order:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['aftersales:order:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <!-- <el-table-column label="ID" align="center" prop="id" /> -->
            <el-table-column label="订单编号" align="center" prop="orderNo" width="150" fixed="left">
                <template slot-scope="scope">
                    <span>{{ scope.row.orderNo }}</span><el-button type="text" size="mini"
                        @click="handleWorkOrder(scope.row.id)">服务情况</el-button>
                </template>
            </el-table-column>
            <el-table-column label="用户姓名" align="center" prop="userName" width="150" />
            <el-table-column label="手机号" align="center" prop="mobile" width="150" />
            <el-table-column label="产品型号" align="center" prop="productModel" width="200" />
            <el-table-column label="用户地址" align="center" prop="address" width="200" />
            <el-table-column label="购买时间" align="center" prop="purchaseTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.purchaseTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="维保结束日期" align="center" prop="elecMonths" width="180">
                <template slot-scope="scope">
                    <span>电子类：{{ scope.row.elecEndDate ? scope.row.elecEndDate :
                        (scope.row.elecMonths ? scope.row.elecMonths + '个月' : '')
                    }}</span><br />
                    <span>配件类：{{ scope.row.partsEndDate ? scope.row.partsEndDate :
                        (scope.row.partsMonths ? scope.row.partsMonths
                            + '个月' : '') }}</span><br />
                    <span>门体：{{ scope.row.doorEndDate ? scope.row.doorEndDate : (scope.row.doorYears ?
                        scope.row.doorYears
                        + '年' : '') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="业务员" align="center" prop="salesman" />
            <el-table-column label="是否包含测量" align="center" prop="measureFlag">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.measure_flag" :value="scope.row.measureFlag" />
                </template>
            </el-table-column>
            <el-table-column label="是否包含安装" align="center" prop="installFlag">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.install_flag" :value="scope.row.installFlag" />
                </template>
            </el-table-column>
            <el-table-column label="是否包含物流" align="center" prop="expressFlag">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.express_flag" :value="scope.row.expressFlag" />
                </template>
            </el-table-column>
            <el-table-column label="订单部" align="center" prop="marketDept" />
            <el-table-column label="工程部" align="center" prop="projectDept" />
            <el-table-column label="制作工厂" align="center" prop="factory" />
            <el-table-column label="是否允许二次预约" align="center" prop="secondTimes">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.second_times_flag" :value="scope.row.secondTimes" />
                </template>
            </el-table-column>
            <el-table-column label="物流单号" align="center" prop="expressNo" />
            <el-table-column label="交期开始时间" align="center" prop="deliveryStartTime" />
            <el-table-column label="交付期限" align="center" prop="deliveryDays">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.product_days" :value="scope.row.deliveryDays" />
                </template>
            </el-table-column>
            <el-table-column label="附件" align="center" prop="attachUrl">
                <template slot-scope="scope">
                    <a :href="scope.row.attachUrl" target="_blank">下载</a>
                </template>
            </el-table-column>
            <el-table-column label="备注" align="center" prop="remarks" />
            <el-table-column label="订单状态" align="center" prop="status" width="150" fixed="right">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.order_status" :value="scope.row.status" />
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150" fixed="right">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['aftersales:order:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleServiceForm(scope.row)">
                        服务预约</el-button>

                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['aftersales:order:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改合同订单对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-row :gutter="10">
                    <el-col :span="12">
                        <el-form-item label="订单编号" prop="orderNo">
                            <el-input v-model="form.orderNo" placeholder="订单编码系统自动生成" :disabled="true"
                                :readonly="true" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="订单状态" prop="status">
                            <el-select v-model="form.status" placeholder="请选择订单状态">
                                <el-option v-for="dict in dict.type.order_status" :key="dict.value" :label="dict.label"
                                    :value="parseInt(dict.value)"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="10">
                    <el-col :span="8">
                        <el-form-item label="用户姓名" prop="userName">
                            <el-input v-model="form.userName" placeholder="请输入用户姓名" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="手机号" prop="mobile">
                            <el-input v-model="form.mobile" placeholder="请输入手机号" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="10">
                    <el-col :span="24">
                        <el-form-item label="用户地址" prop="address">
                            <el-input v-model="form.address" placeholder="请输入用户地址" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="10">
                    <el-col :span="8">
                        <el-form-item label="产品型号" prop="productModel">
                            <el-input v-model="form.productModel" placeholder="请输入产品型号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="购买时间" prop="purchaseTime">
                            <el-date-picker clearable v-model="form.purchaseTime" type="date" value-format="yyyy-MM-dd"
                                placeholder="请选择购买时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="10">
                    <el-col :span="8">
                        <el-form-item label="是否包测量" prop="measureFlag">
                            <el-radio-group v-model="form.measureFlag">
                                <el-radio v-for="dict in dict.type.measure_flag" :key="dict.value"
                                    :label="parseInt(dict.value)">{{
                                        dict.label }}</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="是否包安装" prop="installFlag">
                            <el-radio-group v-model="form.installFlag">
                                <el-radio v-for="dict in dict.type.install_flag" :key="dict.value"
                                    :label="parseInt(dict.value)">{{
                                        dict.label }}</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="是否包物流" prop="expressFlag">
                            <el-radio-group v-model="form.expressFlag">
                                <el-radio v-for="dict in dict.type.express_flag" :key="dict.value"
                                    :label="parseInt(dict.value)">{{
                                        dict.label }}</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="10">
                    <el-col :span="8">
                        <el-form-item label="是否允许二次预约" prop="secondTimes">
                            <el-radio-group v-model="form.secondTimes">
                                <el-radio v-for="dict in dict.type.second_times_flag" :key="dict.value"
                                    :label="parseInt(dict.value)">{{
                                        dict.label }}</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>


                <!-- <el-form-item label="维保结束日期" prop="serviceEndDate">
                    <el-date-picker clearable v-model="form.serviceEndDate" type="date" value-format="yyyy-MM-dd"
                        placeholder="请选择维保结束日期">
                    </el-date-picker>
                </el-form-item> -->
                <el-row :gutter="10">
                    <el-col :span="8">
                        <el-form-item label="订单部" prop="marketDept">
                            <treeselect v-model="form.marketDept" :options="enabledDeptOptions" :show-count="true"
                                placeholder="请选择订单部门" @input="handleDeptChange" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="业务员" prop="salesmanId">
                            <el-select v-model="form.salesmanId" placeholder="请选择业务员" clearable
                                @change="handleUserChange">
                                <el-option v-for="item in userOptions" :key="item.userId" :label="item.nickName"
                                    :value="item.userId">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="10">
                    <el-col :span="8">
                        <el-form-item label="工程部" prop="projectDept">
                            <treeselect v-model="form.projectDept" :options="enabledDeptOptions" :show-count="true"
                                placeholder="请选择工程部门" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">

                        <el-form-item label="制作工厂" prop="factory">
                            <el-input v-model="form.factory" placeholder="请输入制作工厂" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="10">
                    <el-col :span="8">
                        <el-form-item label="交付期限" prop="deliveryDays">
                            <el-select v-model="form.deliveryDays" placeholder="请选择交付期限">
                                <el-option v-for="dict in dict.type.product_days" :key="dict.value" :label="dict.label"
                                    :value="parseInt(dict.value)"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="交期开始时间" prop="deliveryStartTime">
                            <el-date-picker clearable v-model="form.deliveryStartTime" type="date"
                                value-format="yyyy-MM-dd" placeholder="请选择交期开始时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="10">
                    <el-col :span="12">
                        <el-form-item label="维保范围" prop="repairRange">
                            <el-input v-model="form.repairRange" type="textarea" placeholder="请输入维保范围" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="10">
                    <el-col :span="8">
                        <el-form-item label="电子类维保期限" prop="elecMonths">
                            <el-input v-model="form.elecMonths" :min="1" :max="100" placeholder="请输入电子类维保期限">
                                <template slot="append">
                                    个月
                                </template>
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="配件类维保期限" prop="partsMonths">
                            <el-input v-model="form.partsMonths" :min="1" :max="100" placeholder="请输入配件类维保期限">
                                <template slot="append">
                                    个月
                                </template>
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="门体维保期限" prop="doorYears">
                            <el-input v-model="form.doorYears" :min="1" :max="100" placeholder="请输入门体维保期限">
                                <template slot="append">
                                    年
                                </template>
                            </el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="10">
                    <el-col :span="8">
                        <el-form-item label="图片" prop="imagesUrl">
                            <image-upload v-model="form.imagesUrl" :limit="3" :file-size="10" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="附件" prop="attachUrl">
                            <file-upload v-model="form.attachUrl" :limit="1" :file-size="10" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="10">
                    <el-col :span="12">
                        <el-form-item label="备注" prop="remarks">
                            <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="物流单号" prop="expressNo">
                            <el-input v-model="form.expressNo" placeholder="请输入物流单号" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <el-dialog title="服务预约" :visible.sync="serviceDialogVisible" width="600px" append-to-body>
            <el-form ref="serviceForm" :model="serviceForm" :rules="serviceFormRules" label-width="120px">
                <el-form-item label="订单编号" prop="orderId">
                    <el-input v-model="serviceForm.orderNo" placeholder="订单编码系统自动生成" :disabled="true"
                        :readonly="true" />
                </el-form-item>
                <el-form-item label="预约类型" prop="type">
                    <el-radio-group v-model="serviceForm.type">
                        <el-radio v-for="type in serviceTypeOptions" :key="type.value" :label="type.value">{{ type.text
                            }}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="联系人" prop="contacts">
                    <el-input v-model="serviceForm.contacts" placeholder="请输入用户姓名" />
                </el-form-item>
                <el-form-item label="联系电话" prop="contactNumber">
                    <el-input v-model="serviceForm.contactNumber" placeholder="请输入手机号" />
                </el-form-item>

                <el-form-item label="预约时间" prop="installTime" v-if="isInstall">
                    <el-date-picker clearable v-model="serviceForm.installTime" type="date" value-format="yyyy-MM-dd"
                        placeholder="请选择预约时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="上门地址" prop="address">
                    <el-input v-model="serviceForm.address" placeholder="请输入上门地址" />
                </el-form-item>
                <el-form-item label="问题图片" prop="images" v-if="isRepair">
                    <image-upload v-model="serviceForm.images" :limit="3" :file-size="5" />

                </el-form-item>
                <el-form-item label="问题视频" prop="video" v-if="isRepair">
                    <file-upload v-model="serviceForm.video" :limit="1" :file-size="30" :fileType="['mp4']" />
                </el-form-item>
                <el-form-item label="问题描述" prop="problem" v-if="isRepair">
                    <el-input v-model="serviceForm.problem" type="textarea" placeholder="请输入问题描述" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitServiceForm">预 约</el-button>
                <el-button @click="cancelServiceForm">取 消</el-button>
            </div>
        </el-dialog>

        <el-dialog title="服务详情" :visible.sync="workOrderDialogVisible" width="600px" append-to-body>
            <el-table :data="workOrder">
                <el-table-column label="服务类型" prop="typeLabel" />
                <el-table-column label="状态" prop="statusLabel" />
                <el-table-column label="完成时间" prop="finishTime" />
                <el-table-column label="服务师傅" prop="workerName">
                    <template slot-scope="scope">
                        <span>{{ scope.row.workerName || '' }}</span><br />
                        <span>{{ scope.row.workerPhone || '' }}</span>
                    </template>
                </el-table-column>
            </el-table>
        </el-dialog>


    </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder, installAgentAdd, repairAgentAdd, queryWorkOrder } from "@/api/aftersales/order";
import { deptTreeSelect } from "@/api/system/dept";
import { listUser } from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
const currentDate = new Date();
const endDate = new Date();
const afterFourDays = new Date(endDate.setDate(endDate.getDate() + 4));
const afterOneYear = new Date(endDate.setDate(endDate.getDate() + 365));
export default {
    name: "Order",
    dicts: ['product_days', 'order_status', 'express_flag', 'second_times_flag', 'install_flag', 'measure_flag', 'comment_status'],
    components: { Treeselect },
    data () {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 合同订单表格数据
            orderList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                orderNo: null,
                userName: null,
                mobile: null,
                salesman: null,
                measureFlag: null,
                installFlag: null,
                expressFlag: null,
                address: null
            },
            // 所有部门树选项
            deptOptions: undefined,
            // 过滤掉已禁用部门树选项
            enabledDeptOptions: undefined,
            // 用户选择参数
            userOptions: [],
            // 表单参数
            form: {},
            // 表单校验
            rules: {

                userName: [
                    { required: true, message: "用户姓名不能为空", trigger: "blur" }
                ],
                mobile: [
                    { required: true, message: "手机号不能为空", trigger: "blur" }
                ],
                purchaseTime: [
                    { required: true, message: "购买时间不能为空", trigger: "blur" }
                ],
                status: [
                    { required: true, message: "订单状态不能为空", trigger: "change" }
                ],
                commentStatus: [
                    { required: true, message: "是否评论不能为空", trigger: "change" }
                ],
                // serviceEndDate: [
                //   { required: true, message: "维保结束日期不能为空", trigger: "blur" }
                // ],
                measureFlag: [
                    { required: true, message: "是否包测量不能为空", trigger: "change" }
                ],
                installFlag: [
                    { required: true, message: "是否包安装不能为空", trigger: "change" }
                ],
                expressFlag: [
                    { required: true, message: "是否包物流不能为空", trigger: "change" }
                ],
                secondTimes: [
                    { required: true, message: "是否允许二次预约不能为空", trigger: "change" }
                ],
            },
            serviceForm: {
            },
            serviceFormRules: {
                orderId: [
                    { required: true, message: "订单ID不能为空", trigger: "blur" }
                ],
                type: [
                    { required: true, message: "预约类型不能为空", trigger: "change" }
                ],
                installTime: [
                    {
                        required: true,
                        message: "预约时间不能为空",
                        trigger: "blur",
                        validator: (rule, value, callback) => {
                            if (this.isInstall && !value) {
                                callback(new Error('预约时间不能为空'));
                            } else {
                                callback();
                            }
                        }
                    }
                ],
                contacts: [
                    { required: true, message: "联系人不能为空", trigger: "blur" }
                ],
                contactNumber: [
                    { required: true, message: "联系电话不能为空", trigger: "blur" }
                ],
                address: [
                    { required: true, message: "预约地址不能为空", trigger: "blur" }
                ],
                problem: [
                    {
                        required: true,
                        message: "问题描述不能为空",
                        trigger: "blur",
                        validator: (rule, value, callback) => {
                            if (this.isRepair && !value) {
                                callback(new Error('问题描述不能为空'));
                            } else {
                                callback();
                            }
                        }
                    }
                ]

            },
            serviceTypeOptions: [{
                value: 0,
                text: '测量'
            },
            {
                value: 1,
                text: '安装'
            },
            {
                value: 2,
                text: '调试'
            },
            {
                value: 3,
                text: '保养'
            },
            {
                value: 4,
                text: '售后'
            },
            ],
            serviceDialogVisible: false,
            workOrder: [],
            workOrderDialogVisible: false
        };
    },
    created () {
        this.getList();
        this.getDeptTree();
        this.getUserTree();
    },
    computed: {
        isInstall () {
            return [0, 1, 2].includes(this.serviceForm.type);
        },
        isRepair () {
            return [3, 4].includes(this.serviceForm.type)
        }
    },
    methods: {
        handleWorkOrder (orderId) {
            console.log(orderId);
            queryWorkOrder({ orderId: orderId }).then(response => {
                this.workOrder = response.data;
                this.workOrderDialogVisible = true;
            });
        },
        handleServiceForm (row) {
            console.log(row, 8888);
            this.resetServiceForm();
            this.serviceForm.orderNo = row.orderNo;
            this.serviceForm.orderId = row.id;
            this.serviceForm.contacts = row.userName;
            this.serviceForm.contactNumber = row.mobile;
            this.serviceForm.address = row.address;
            this.serviceDialogVisible = true;
        },
        submitServiceForm () {
            this.$refs["serviceForm"].validate(valid => {
                if (valid) {
                    if (this.isInstall) {
                        installAgentAdd(this.serviceForm).then(response => {
                            this.$modal.msgSuccess("预约成功,可前往安装服务查看！");
                            this.serviceDialogVisible = false;
                        });
                    } else if (this.isRepair) {
                        repairAgentAdd(this.form).then(response => {
                            this.$modal.msgSuccess("预约成功,可前往维保服务查看！");
                            this.serviceDialogVisible = false;
                        });
                    }
                }

            })
        },
        cancelServiceForm () {
            this.serviceDialogVisible = false;
            this.resetServiceForm()
        },
        /** 查询用户下拉树结构 */
        getUserTree () {
            const params = { pageSize: 100 };
            console.log(66666, this.form.marketDept);
            if (this.form.marketDept) {
                params.deptId = this.form.marketDept;
            }
            listUser(params).then(response => {
                this.userOptions = response.rows;
            });
        },
        handleUserChange (userId) {
            if (userId) {
                const name = this.userOptions.find(item => item.userId === userId).nickName;
                this.form.salesman = name;
            } else {
                this.form.salesman = null;
            }

        },
        handleDeptChange (value) {
            this.getUserTree();
        },
        /** 查询部门下拉树结构 */
        getDeptTree () {
            deptTreeSelect().then(response => {
                this.deptOptions = response.rows;
                this.enabledDeptOptions = this.filterDisabledDept(JSON.parse(JSON.stringify(response.data)));

            });
        },
        // 过滤禁用的部门
        filterDisabledDept (deptList) {
            return deptList.filter(dept => {
                if (dept.disabled) {
                    return false;
                }
                if (dept.children && dept.children.length) {
                    dept.children = this.filterDisabledDept(dept.children);
                }
                return true;
            });
        },
        /** 查询合同订单列表 */
        getList () {
            this.loading = true;
            listOrder(this.queryParams).then(response => {
                this.orderList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel () {
            this.open = false;
            this.reset();
        },
        resetServiceForm () {
            this.serviceForm = {
                orderId: 0,
                orderNo: null,
                type: 0,
                installTime: afterFourDays,
                contacts: null,
                contactNumber: null,
                address: null,
                video: null,
                images: null
            },
                this.resetForm("serviceForm");
        },
        // 表单重置
        reset () {
            this.form = {
                id: null,
                orderNo: null,
                openId: null,
                userName: null,
                mobile: null,
                productModel: null,
                address: null,
                purchaseTime: currentDate,
                status: 0,
                commentStatus: null,
                expressNo: null,
                deliveryDays: null,
                serviceEndDate: null,
                salesmanId: null,
                salesman: null,
                measureFlag: 1,
                installFlag: 1,
                expressFlag: 1,
                marketDept: null,
                projectDept: null,
                factory: null,
                secondTimes: 0,
                deliveryStartTime: null,
                remarks: null,
                attachUrl: null,
                imagesUrl: null,
                createTime: null,
                updateTime: null,
                elecMonths: 12,
                partsMonths: 12,
                doorYears: 10,
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery () {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery () {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange (selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd () {
            this.reset();
            this.open = true;
            this.title = "添加合同订单";
        },
        /** 修改按钮操作 */
        handleUpdate (row) {
            this.reset();
            const id = row.id || this.ids
            getOrder(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改合同订单";
            });
        },
        /** 提交按钮 */
        submitForm () {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateOrder(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addOrder(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete (row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除合同订单编号为"' + ids + '"的数据项？').then(function () {
                return delOrder(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport () {
            this.download('aftersales/order/export', {
                ...this.queryParams
            }, `order_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
